const TOKEN_KEY_NAME = 'XIAOLV_YOUCHUANG';

interface IUseToken {
	getTokenSync: () => string;
	setTokenSync: (token: string) => void;
	removeTokenSync: () => void;
	getToken: () => Promise<string>;
	setToken: (token: string) => Promise<string | any>;
	removeToken: () => Promise<any | void>;
}

function useToken(): IUseToken {
	const getTokenSync = (): string => {
		return uni.getStorageSync(TOKEN_KEY_NAME)
	}
	const setTokenSync = (token: string): void => {
		uni.setStorageSync(TOKEN_KEY_NAME, token);
	}
	const removeTokenSync = (): void => {
		return uni.removeStorageSync(TOKEN_KEY_NAME);
	}
	const getToken = (): Promise<string> => {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: TOKEN_KEY_NAME,
				success(res) {
					resolve(res);
				},
				fail(err) {
					reject(err);
				}
			})
		})
	}
	const setToken = (token: string): Promise<string | void> => {
		return new Promise((resolve, reject) => {
			uni.setStorage({
				key: TOKEN_KEY_NAME,
				data: token,
				success(res) {
					resolve(res);
				},
				fail(err) {
					reject(err);
				}
			})
		})
	}
	const removeToken = (): Promise<string | void> => {
		return new Promise((resolve, reject) => {
			uni.removeStorage({
				key: TOKEN_KEY_NAME,
				success(res) {
					resolve(res);
				},
				fail(err) {
					reject(err);
				}
			})
		})
	}

	return {
		getTokenSync,
		setTokenSync,
		removeTokenSync,
		getToken,
		setToken,
		removeToken,
	}
}


export {
	useToken
}